{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 模型构造\n",
    "## 保存张量与网络"
   ],
   "id": "4d3c465409214e09"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 加载和保存张量",
   "id": "59ff87d533ec9c5d"
  },
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-16T13:48:50.768302Z",
     "start_time": "2025-08-16T13:48:49.251402Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as f"
   ],
   "id": "5e8328f3cdc40bf",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:51:10.699397Z",
     "start_time": "2025-08-16T13:51:10.694393Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.arange(10)\n",
    "print(x)\n",
    "torch.save(x,'./data/x-file.pickle')"
   ],
   "id": "2e88833546c06460",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:52:31.777067Z",
     "start_time": "2025-08-16T13:52:31.772999Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x2 = torch.load('./data/x-file.pickle')\n",
    "print(x2)"
   ],
   "id": "5ef3cacd2934e0b0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 加载和保存模型参数",
   "id": "578fd62874764af5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:55:52.916350Z",
     "start_time": "2025-08-16T13:55:52.912935Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class MLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.hidden = nn.Linear(20,256)\n",
    "        self.output = nn.Linear(256,10)\n",
    "    def forward(self,x):\n",
    "        return self.output(f.relu(self.hidden(x)))"
   ],
   "id": "c284f34caa542cc6",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:55:54.473703Z",
     "start_time": "2025-08-16T13:55:54.467752Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = MLP()\n",
    "X = torch.randn(size=(2,20))\n",
    "Y = net(X)"
   ],
   "id": "93fb1c65313cf239",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:56:20.428461Z",
     "start_time": "2025-08-16T13:56:20.421979Z"
    }
   },
   "cell_type": "code",
   "source": "torch.save(net.state_dict(),'./data/mlp.params.pickle')",
   "id": "a46ded0113324d4d",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-16T13:59:46.911231Z",
     "start_time": "2025-08-16T13:59:46.906038Z"
    }
   },
   "cell_type": "code",
   "source": [
    "clone = MLP()\n",
    "clone.load_state_dict(torch.load('./data/mlp.params.pickle'))\n",
    "clone.eval()"
   ],
   "id": "1770a344f64538d1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MLP(\n",
       "  (hidden): Linear(in_features=20, out_features=256, bias=True)\n",
       "  (output): Linear(in_features=256, out_features=10, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
